package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.entity.AddressBook;
import com.itheima.entity.Orders;

import com.itheima.service.AddressBookService;
import com.itheima.service.OrderService;
import com.itheima.service.UserService;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    UserService userService;

    @Autowired
    AddressBookService addressBookService;

    @PostMapping("/submit")
    public R<String> sub(@RequestBody Orders orders) {

        orderService.submit(orders);

        return R.success("下单成功");

    }

    /**
     * 用户订单分页查询
     */
    @GetMapping("/userPage")
    public R<Page> get(int page, int pageSize) {

        // 获取当前用户的id
        Long currentId = BaseContext.getCurrentId();
// 创建分页查询
        Page<Orders> ordersPage = new Page<>(page, pageSize);
//  通过 id 条件查询所属订单  订单从属 某账号
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId, currentId);

        Page<Orders> page1 = orderService.page(ordersPage, ordersLambdaQueryWrapper);

        return R.success(ordersPage);

    }

    /**
     * 后台订单分页查询
     */
    @GetMapping("/page")
    public R<Page> get(int page, int pageSize, Orders orders, String  beginTime, String endTime) {
        // 分页查询构建
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //   返回页面 分页查询
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);

        //条件查询构建
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 订单号匹配
        ordersLambdaQueryWrapper.eq(StringUtils.isNotEmpty(orders.getNumber()), Orders::getNumber, orders.getNumber())
                // 日期范围匹配
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime, beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime, endTime);
        // 查询
        Page<Orders> page1 = orderService.page(ordersPage, ordersLambdaQueryWrapper);

        List<OrdersDto> ordersDtos = page1.getRecords().stream().map((item) -> {

            OrdersDto ordersDto = new OrdersDto();
            // 每个单个的 订单数据 赋值给 回显页面数据
            BeanUtils.copyProperties(item, ordersDto);

            Long Id = item.getAddressBookId();

            AddressBook addressBook = addressBookService.getById(Id);

            ordersDto.setUserName(addressBook.getConsignee());

            return ordersDto;

        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(ordersDtos);

        return R.success(ordersDtoPage);

    }

    /**
     * 订单状态修改
     */

    @PutMapping
    public R<String> update(Orders orders) {

        LambdaUpdateWrapper<Orders> ordersLambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        ordersLambdaUpdateWrapper.set(Orders::getStatus, 3);

        orderService.update(ordersLambdaUpdateWrapper);

        return R.success("更改为派送成功");

    }

}
